package fr.mywiki.view.web.struts.action.file;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;

import javax.ejb.ObjectNotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

import fr.mywiki.business.file.FileManager;
import fr.mywiki.business.resources.ResourceManager;
import fr.mywiki.model.ejbAdapter.utils.PersistenceException;
import fr.mywiki.view.web.struts.form.file.ManageFileForm;

/**
 * Dispatch action class for the file management.
 */
public class ManageFileAction extends DispatchAction {
	/**
	 * Name of the request attribute which contains the list of files to
	 * display. It is a <code>ArrayList</code> of <code>FileLite</code>
	 * objects.
	 */
	public static final String FILES_LIST = "filesListAttribute";
	
	/**
	 * Displays standard formular to select a file.
	 */
	public ActionForward display(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3)
			throws RemoteException {
		ManageFileForm form = (ManageFileForm) actionForm;
		form.setFileMessage(new String());
		form.initFilesList();
		
		return mapping.findForward("display");
	}
	
	/**
	 * Uploads the file which is contained in the FormFile object. There must not
	 * be another file with the same name.
	 * 
	 * TODO test
	 */
	public ActionForward uploadFile(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse arg3)
			throws FileNotFoundException, IOException, ObjectNotFoundException,
			PersistenceException {
		ManageFileForm form = (ManageFileForm) actionForm;
		
		FormFile formFile = form.getFormFile();
		
		String fileName = form.getFileName();
		if (fileName == null || fileName.length() < 1)
			fileName = formFile.getFileName();
		
		if(FileManager.findFileByName(fileName) != null) {
			// There is already a file having this name - error
			ActionMessages errors = new ActionMessages();
			errors.add(ActionMessages.GLOBAL_MESSAGE,
					new ActionMessage("file.upload.samename"));
			saveErrors(request, errors);
			return mapping.findForward("display");
		}
		byte[] data = formFile.getFileData();
		if (data != null) {
			if (data.length > 1048576) {
				// The file is too long
				ActionMessages errors = new ActionMessages();
				errors.add(ActionMessages.GLOBAL_MESSAGE,
						new ActionMessage("file.upload.toolong"));
				saveErrors(request, errors);
				return mapping.findForward("display");
			}
			FileManager.uploadFile(fileName, data);
		}
		
		form.setFileMessage(ResourceManager.getMessage("file.upload.done",
				request.getLocale()));
		form.setFilesList(FileManager.getAllFilesLight());
		
		return mapping.findForward("display");
	}
}
